<?php
namespace app\entries\model;
use think\Model;

/**
 *
 */
class Jren extends Model
{
    //修改匠人信息
    function modifyJren($id, $data)
    {
        $retval = db('jiangren')->where('id',$id)->update($data);
        // return 1;
        return $retval;
    }

    //添加匠人数据（单个）
    function addjr($data)
    {
        $retval = db('jiangren')->insert($data);
        //关联茶类和产区
        $condition = [
            "teacase_id"=>$data['teacase_id'],
            "teaarea_id"=>$data['teaarea_id'],
        ];
        $res = db('case_area')->where($condition)->find();
        if(!$res){
            db('case_area')->insert($condition);
        }
        // return 1;
        return $retval;
    }

    //删除匠人信息
    function delJr($id)
    {
        $retval = db("jiangren")->where("id", $id)->delete();
        // return 1;
        return $retval;
    }

    //上传的匠人的Excel数据存入数据库
    function insertExcel($data)
    {
        $teacase_ids = ids_teacase();//茶类名称和id的关联数组
        $teaarea_ids = ids_teaarea();//茶类产区和id的关联数组
        $bangdan_ids = ids_bangdan();//榜单名称和id的关联数组

        //Excel数据插入数据表
        foreach ($data as $onedata) {
            if(!array_key_exists($onedata["teacase"], $teacase_ids)){
                continue;
            }
            if(!array_key_exists($onedata["bangdan"], $bangdan_ids)||$bangdan_ids[$onedata['bangdan']]!=1){
                continue;
            }
            $insertdata =[];

            //判断茶产地是否在已经备案
            if(!array_key_exists($onedata["teaarea"], $teaarea_ids)){
                db("teaarea")->insert(["area"=>$onedata["teaarea"]]);
                $newId = db("teaarea")->getLastInsID();
                $teaarea_ids[$onedata["teaarea"]] = $newId;
                //关联茶类和茶产地
                $insCaseArea['teacase_id'] = $teacase_ids[$onedata["teacase"]];
                $insCaseArea['teaarea_id'] = $newId;
                db('case_area')->insert($insCaseArea);
                $teaarea_ids[$onedata["teaarea"]] = $newId;
            }

            $insertdata =[
                "teacase_id" => $teacase_ids[$onedata['teacase']],
                "teaarea_id" => $teaarea_ids[$onedata['teaarea']],
                "bangdan_id" => $bangdan_ids[$onedata['bangdan']],
                "name" =>$onedata['name'],
                "score" =>$onedata['score'],
                "briefintro" =>$onedata['briefintro'],
                "contact" =>$onedata['contact'],
                "phone" =>$onedata['phone'],
                "wechat" =>$onedata['wechat'],
                "detailintro" =>$onedata['detailintro'],
                "addtime" => time()
            ];
            db("jiangren")->insert($insertdata);
        }
        return 1;
    }

    //返回某茶类对应的产地
    function reTeaArea($teacase_id)
    {
       $res = db("case_area")->field("teaarea_id")->where("teacase_id",$teacase_id)->select();
       return $res;
    }

    //返回所有匠人信息
    function getAlljr()
    {
        $res = db("jiangren")->order('score desc')->select();
        return $res;
    }

    //返回未上榜匠人信息
    function getOther()
    {
        $res = db("jiangren")->where('bangdan_id',4)->select();
        return $res;
    }










}


?>